﻿/*=====================================================================
 TASK 6:                                                      {Methods}
 Write a method that returns the index of the first element in array 
 that is bigger than its neighbors, or -1, if there’s no such element.
 Use the method from the previous exercise.
 =====================================================================*/

using System;

class FirstElementBiggerThanNeighbors
{
    static void Main()
    {
        int index;
        int[] arr = { 1, 2, 3, 4, 5, 5, 5 };

        index = FirstBiggerThanNeighbors(arr);

        if (index == -1)
        {
            Console.WriteLine("cod:-1\nthere’s no such element");
        }
        else
        {
            Console.WriteLine("element with index={0} : arr[{0}]={1}\nis first element bigger than its neighbors", index, arr[index]);
        }
    }

    static int FirstBiggerThanNeighbors(int[] arr)
    {
        int first = arr.Length - 1;

        if (arr[0] > arr[1])
        {
            first = 0;
            return first;
        }
        else if (arr[0] <= arr[1])
        {
            for (int i = 0; i < arr.Length - 2; i++)
            {
                if (arr[i + 1] > arr[i] && arr[i + 1] > arr[i + 2])
                {
                    first = i + 1;
                    return first;
                }
                else
                {
                    first = 0;
                }
            }
            
        }

        if ((first == 0) && (arr[arr.Length - 1] > arr[arr.Length - 2]))
        {
            first = arr.Length - 1;
        }
        else
        {
            first = -1;
        }

        return first;
    }
}

